package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    //查询所有
    @GetMapping("/category/findAll")
    public ResultInfo findAll(){
        List<Category> categoryList = categoryService.findAll();
        return  ResultInfo.success(categoryList);
    }
    //2.新增
    //调用Service保存
    @PostMapping("/category")
    public ResultInfo save(@RequestBody Category category){
        categoryService.save(category);
        return ResultInfo.success(null);
    }
    //3.修改内容
    public ResultInfo update(@RequestBody Category category){
        categoryService.update(category);
        return ResultInfo.success(null);
    }
    //4.删除内容
    //根据主键删除
    public ResultInfo deleteById(Long id){
        ResultInfo resultInfo = categoryService.deleteById(id);
        return resultInfo;
    }
    @GetMapping("/category/list")
    public ResultInfo findByType(Integer type){
        List<Category > categoryList = categoryService.findByType(type);
        return ResultInfo.success(categoryList);
    }

}
